package com.sniff.atp.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.sniff.atp.constant.LoginConstant;
import com.sniff.atp.dto.SystemLoginDTO;
import com.sniff.atp.entity.ToolsInfo;
import com.sniff.atp.manager.ToolsInfoMananager;
import com.sniff.atp.response.SniffResponse;
import com.sniff.atp.service.login.SystemLoginService;
import com.sniff.atp.service.order.PurchaseAllotOrder;
import com.sniff.atp.service.purchase.OEMPurchaseService;
import com.sniff.atp.service.purchase.PurchaseMockService;
import com.sniff.atp.service.purchase.PurchaseService;
import com.sniff.atp.utils.JsonUtil;
import com.sniff.atp.utils.ToolsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;

@Api(value = "采购模块", tags = "采购模块")
@RestController
@RequestMapping("/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseAllotOrder purchaseAllotOrder;
    @Autowired
    private PurchaseService purchaseService;
    @Autowired
    private OEMPurchaseService oemPurchaseService;

    @Autowired
    private SystemLoginService systemLoginService;
    @Autowired
    private ToolsInfoMananager toolsInfoMananager;
    @Autowired
    private PurchaseMockService purchaseMockService;

    public SystemLoginDTO login() {
        return systemLoginService.login("admin", "888888");
    }

    @ApiOperation("根据订单号，查询采购单号")
    @GetMapping("/get")
    public Object get(@RequestParam String platformOrderNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(7L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        String result = purchaseAllotOrder.getPurchaseOrderNo(platformOrderNo);
        return SniffResponse.pass(JsonUtil.strToJSONObject(result));
    }

    @ApiOperation("已分单，根据订单号，执行采购流程")
    @GetMapping("/allot/doFlow")
    public Object allotDoFlow(@RequestParam String platformOrderNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(8L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        String result = purchaseAllotOrder.getPurchaseOrderNo(platformOrderNo);
        JSONArray jsonArray = JsonUtil.extractJSONArray(result, "purchaseNo");

        JSONObject jsonObject1 = new JSONObject();

        JSONArray objects = new JSONArray();

        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject jsonObject = new JSONObject();
            String purchaseNo = jsonArray.getString(i);
            if (purchaseNo.contains("DHCGD")) {
                oemPurchaseService.oemPurchaseProcess(login(), purchaseNo);
                Object logisticsNumber = ToolsUtil.getStringBuild("oemLogisticsNumber");
                jsonObject.put("logisticsNumber", logisticsNumber);
            }else {
                purchaseService.puchasePrecess(login(), purchaseNo);
                Object logisticsNumber = ToolsUtil.getStringBuild("logisticsNumber");
                jsonObject.put("logisticsNumber", logisticsNumber);
            }

            jsonObject.put("purchaseNo", purchaseNo);
            objects.add(jsonObject);
        }
        jsonObject1.put("purchaseInfo", objects);
        return SniffResponse.pass(jsonObject1);
    }

    @ApiOperation("未分单，根据订单号，执行采购流程")
    @GetMapping("/unAllot/doFlow")
    public Object unAllotDoFlow(@RequestParam String platformOrderNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(9L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
//        purchaseAllotOrder.allotAutoOrder();

        purchaseService.allotAutoOrder(LoginConstant.systemLoginDTO, platformOrderNo);
        return allotDoFlow(platformOrderNo);
    }

    @ApiOperation("根据采购单号，执行采购流程")
    @GetMapping("/allot/purchase")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNos", value = "采购单号"),
            @ApiImplicitParam(name = "node", value = "操作节点: 0.待分配，1 待换源，2 待下单，3 待支付，4 代发货，5 待收货，6 已完成")})
    public Object unAllotDoFlowNode(@RequestParam String purchaseNos, @RequestParam Integer node) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(10L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        String[] split = purchaseNos.split(",");

        HashMap<String, Object> re = new HashMap<>();

        for (String purchaseNo : split) {
            Object o = purchaseService.purchaseAllProcess(LoginConstant.systemLoginDTO, purchaseNo, node);
            if( node > 4 && !purchaseNo.contains("DKCGD") ) {
                Object logisticsNumber = ToolsUtil.getStringBuild("logisticsNumber");
                ((JSONObject) o).put("logisticsNumber", logisticsNumber);
            }

            re.put(purchaseNo, o);
        }

        return SniffResponse.pass(re);
    }

    @ApiOperation("根据订单号，一键分单")
    @GetMapping("/allotNew")
    @ApiImplicitParams({@ApiImplicitParam(name = "platformOrderNo", value = "多个订单用英文逗号隔开")})
    public Object allotNew(@RequestParam String platformOrderNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(11L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        String s;
        String[] platformOrderNos;
        if (platformOrderNo.contains(",")){
            platformOrderNos = platformOrderNo.split(",");
            s = purchaseService.allotAutoOrder(LoginConstant.systemLoginDTO, platformOrderNos);
        } else {
            s = purchaseService.allotAutoOrder(LoginConstant.systemLoginDTO, platformOrderNo);
        }
        return  JSON.parseObject(s);
    }

    @ApiOperation("采购Mock下单失败")
    @GetMapping("/createPurchaseOrderFail")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNo", value = "多个订单用英文逗号隔开")})
    public Object createPurchaseOrderFail(@RequestParam String purchaseNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(42L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        purchaseMockService.placingOrder(LoginConstant.systemLoginDTO, purchaseNo);
        Object purchaseOrderFail = purchaseMockService.createPurchaseOrderFail(LoginConstant.systemLoginDTO, purchaseNo);
        return  purchaseOrderFail;
    }

    @ApiOperation("采购Mock下单成功")
    @GetMapping("/createPurchaseOrderSuccess")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNo", value = "多个订单用英文逗号隔开")})
    public Object createPurchaseOrderSuccess(@RequestParam String purchaseNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(43L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        purchaseMockService.placingOrder(LoginConstant.systemLoginDTO, purchaseNo);
        Object purchaseOrderFail = purchaseMockService.createPurchaseOrderSuccess(LoginConstant.systemLoginDTO, purchaseNo);
        return  purchaseOrderFail;
    }

    @ApiOperation("采购Mock议价")
    @GetMapping("/negotiated")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNo", value = "输入采购单号")})
    public Object negotiated(@RequestParam String purchaseNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(44L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);

        Object purchaseOrderFail = purchaseMockService.negotiated(LoginConstant.systemLoginDTO, purchaseNo);
        return  purchaseOrderFail;
    }

    @ApiOperation("采购Mock支付")
    @GetMapping("/negotiatedPay")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNo", value = "输入采购单号")})
    public Object negotiatedPay(@RequestParam String purchaseNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(45L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        Object purchaseOrderFail = purchaseMockService.negotiatedPay(LoginConstant.systemLoginDTO, purchaseNo);
        return  purchaseOrderFail;
    }

    @ApiOperation("采购Mock回填物流")
    @GetMapping("/negotiatedLogistics")
    @ApiImplicitParams({@ApiImplicitParam(name = "purchaseNo", value = "输入采购单号")})
    public Object negotiatedLogistics(@RequestParam String purchaseNo) {
        ToolsInfo toolsInfo = toolsInfoMananager.selectOne(46L);
        toolsInfo.setCount(toolsInfo.getCount() + 1);
        toolsInfoMananager.updateOne(toolsInfo);
        Object purchaseOrderFail = purchaseMockService.negotiatedLogistics(LoginConstant.systemLoginDTO, purchaseNo);
        return  purchaseOrderFail;
    }
}
